package com.innoking.docsys.utils;


import com.innoking.docsys.user.module.UpmsOrganizationVO;
import java.util.ArrayList;
import java.util.List;

/**
 * 树形结构Util
 * zry
 * 2019-09-10
 */
public class TreeUtil {
    public static List<UpmsOrganizationVO> OrgList(List<UpmsOrganizationVO> treeNodes){
        List<UpmsOrganizationVO> trees = new ArrayList<UpmsOrganizationVO>();
        for (UpmsOrganizationVO treeNode : treeNodes) {
            int pid = treeNode.getPid();
            if (pid == 0) {
                trees.add(findAddressChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }
    /**
     * 递归查找地址子节点
     * @param treeNodes
     * @return
     */
    public static UpmsOrganizationVO findAddressChildren(UpmsOrganizationVO treeNode,List<UpmsOrganizationVO> treeNodes){
        for (UpmsOrganizationVO it : treeNodes) {
            int pid = it.getPid();
            int organizationId = treeNode.getOrganizationId();
            if(organizationId == pid) {
                if (treeNode.getChildren() == null){
                    treeNode.setChildren(new ArrayList<UpmsOrganizationVO>());
                }
                treeNode.getChildren().add(findAddressChildren(it,treeNodes));
            }
        }
        return treeNode;
    }

}
